/**
 * DialogResults
 * ---------------------------------------
 * 
 * - This class will inform results of validations.
 * 
 * @author Caio Thomas
 * @version 25-10-2013  
 */
package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextPane;

import model.constraints.AbstractConstraint;
import model.validator.ObserverSaxParser;

public class DialogResults extends JDialog implements ActionListener {

	private static final long serialVersionUID = 1L;
	private JButton btnOK = new JButton("Ok");	
    private JPanel botoes = new JPanel();    
	private JTabbedPane tabbedPane;    
    private JTextPane automataText = new JTextPane();
    private JTextPane pathText = new JTextPane();
    private JPanel panelPath = new JPanel();  
	private JPanel panelConstraint = new JPanel();  
	private JPanel panelAutomata = new JPanel();
	
    //constraint result
    @SuppressWarnings("unused")
	private JTextPane constraint = new JTextPane();
	private JTextArea constraintResults = new JTextArea(5, 15);	
	private JTextArea constraintError = new JTextArea(5, 5);
	
	private Initial initial = null;	
	private DialogForm dialog = null;
	private ObserverSaxParser validateSaxParse;
	
	public DialogResults(int posicao, Initial i) {		
		
		initial = i;
		validateSaxParse = i.validate.getConstraints().get(posicao);		
		dialog = i.singleDialogXDF();
		    	
		btnOK.setPreferredSize(new Dimension(100, 25));
        btnOK.addActionListener(this);        
        
        //tab constraint
    	panelConstraint.setLayout(new GridLayout(2, 1, 5, 5));

    	//text result
    	JPanel panelResult = new JPanel();  
    	panelResult.setBorder(BorderFactory.createTitledBorder("Result"));
    	panelResult.setLayout(new BorderLayout());       	
    	constraintResults.setEditable(false);     	
    	panelResult.add(new JScrollPane(constraintResults));   
    	
    	//text error
    	JPanel panelError = new JPanel();  
    	panelError.setBorder(BorderFactory.createTitledBorder("Error"));    	
    	panelError.setLayout(new BorderLayout());    
    	constraintError.setEditable(false); 
    	constraintError.setForeground(Color.RED);
    	panelError.add(new JScrollPane(constraintError));    	    	
    	    	
    	panelConstraint.add(panelResult, BorderLayout.NORTH);
    	panelConstraint.add(panelError,  BorderLayout.SOUTH);
    	
    	//tab automata    	
    	panelAutomata.setLayout(new BorderLayout());
    	automataText.setContentType("text/html");
    	automataText.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
    	automataText.setEditable(false);    	
    	panelAutomata.add(new JScrollPane(automataText));    	
    	
    	//tab constraint
    	panelPath.setLayout(new BorderLayout());
    	pathText.setContentType("text/html");
    	pathText.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
    	pathText.setEditable(false);    	
    	panelPath.add(new JScrollPane(pathText));    	
    	            	
    	        
    	tabbedPane = new JTabbedPane();
    	tabbedPane.addTab("Validation", panelConstraint);
    	tabbedPane.addTab("Automata", panelAutomata);    
        tabbedPane.addTab("Constraint", panelPath);	
        
        AbstractConstraint ab = (AbstractConstraint) validateSaxParse.getConstraint();
        //set results
    	constraintResults.setText(validateSaxParse.getResult().toString());
    	constraintError.setText(validateSaxParse.getErrosConstraint().toString());
    	automataText.setText(ab.toString());
    	pathText.setText(ab.toStringPath());
    	
        botoes.add(btnOK);       
        botoes.add(Box.createVerticalStrut(45));
        botoes.setBorder(BorderFactory.createMatteBorder(1, 0, 0, 0,Color.LIGHT_GRAY));
        
        setTitle("Validation Result");
        add(tabbedPane, BorderLayout.CENTER);        
        add(botoes, BorderLayout.SOUTH);
        setSize(600, 450);
        setLocation((java.awt.Toolkit.getDefaultToolkit()
                                        .getScreenSize().width / 2)
                                        - (this.getWidth() / 2), (java.awt.Toolkit
                                        .getDefaultToolkit().getScreenSize().height / 2)
                                        - (this.getHeight() / 2));
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);            
	}

	/**
	 * This function will edit Constraint inserted
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
        Object origem = e.getSource();

        if(origem == btnOK)
            this.dispose();		
	}

	public void setInitial(Initial initial) {
		this.initial = initial;
	}

	public Initial getInitial() {
		return initial;
	}

	public void setDialog(DialogForm dialog) {
		this.dialog = dialog;
	}

	public DialogForm getDialog() {
		return dialog;
	}
}